<?php
class params_controlls_http_web extends controller_prototype_class
{
    protected $_templateFolder = 'default';
    protected $_fieldArrName = 'params';
    protected $_params = array();
    
    protected function ControllerInit ()
    {
		 
    }    
    
    /**
		устанавливает активную папку с шаблонами полей
     */
    public function SetTemplateFolder ($folder)
    {
        if (!$folder) return $this;
        $this->_templateFolder = $folder;
        return $this;
    }

    public function GetTemplateFolder()
    {
        return $this->_templateFolder;
    }
        
    /**
		устанавливает имя полей в шаблонах
     */
    public function SetFieldArrName($fieldArrName)
    {
        if (! $fieldArrName) return $this;
        $this->_fieldArrName = $fieldArrName;
        return $this;
    }   
    
    public function GetFieldArrName()
    {
        return $this->_fieldArrName;
    }    
    
    protected function GetFieldHandler($config)
    {
    	$fieldType = $config['type'];
    	
    	$fieldClassName = 'ParamsField' .$fieldType;
    	if (!class_exists($fieldClassName))
    	{
    		$filePath = _APP_PATH. "modules/controlls/controllers/params/fields/params_field_" .strtolower($fieldType). ".php";
    		if (!is_file($filePath)) throw new Exception('Файл ' .$filePath. ' не найден');
    		
    		include_once($filePath);
    	}
    	
    	return new $fieldClassName($config); 
    }
    
    public function PreparePostData(MozgParams $fieldPostData)
    {
    	if (!$fieldPostData->IsSetConfig()) return $fieldPostData; 
    	
    	$data = $fieldPostData->GetData();
		
    	$newData = array();
		foreach ($data as $fieldKey => $fieldValue)
		{
			$configField = $fieldPostData->GetConfigField($fieldKey);
			if (!$configField) continue;
			
			$newData[$fieldKey] = $this->GetFieldHandler($configField)->PreparePostData($fieldValue);
		}    

		return $newData;
    }
    
    public function GetFieldHtml($fieldName, $fieldConfig, $fieldValue = null)
    {   
		$handler = $this->GetFieldHandler($fieldConfig);
		
        $t = $this->GetTemplator();
        $t->data = $handler->PrepareOutData($fieldValue);
        $t->params = $fieldConfig;
        $t->fieldName = $fieldName;
        $t->fieldArrName = $this->GetFieldArrName();
        
        return $t->render('params/' . $this->_templateFolder . '/fields/_' . $fieldConfig['type'] . '.phtml');		
    }

    public function GetFullHtml(MozgParams $fieldParams)
    {
    	if (!$fieldParams->IsSetConfig()) return '';
    	 
		$data = $fieldParams->GetData();
		
		$htmlFields = array();
		foreach ($data as $fieldKey => $fieldValue)
		{
			$configField = $fieldParams->GetConfigField($fieldKey);
			if (!$configField) continue;
						
			$html = $this->GetFieldHtml($fieldKey, $configField, $fieldValue);
			if (false === $html) continue;
			
			$htmlFields[$fieldKey] = $html;
		}	
		
        $t = $this->GetTemplator();
        $t->htmlFields = $htmlFields;
        $t->configFields = $fieldParams->GetConfigFields();
        $t->configTitle = $fieldParams->GetConfigTitle();
        $t->fieldArrName = $this->GetFieldArrName();
        $t->fieldParamsPath = $fieldParams->GetConfigPath();
        $t->fieldTemplate = $this->GetTemplateFolder();
        
        return $t->render('params/' . $this->_templateFolder . '/_block.phtml');		
    }
    
    public function GetParamsFieldsJS()
    {
        $t = $this->GetTemplator();
        return $t->render('params/_fields_js.phtml');    	
    }
}